<?php

// admin users edit page
require_once 'view/common/layout.formview.php';
require_once 'view/common/layout.view.php';

require_once 'view/adm/users/registered.index.php';
require_once 'control/adm/users/registered/form.php';

class RegisteredUsersDeletePage extends PageController {

	var $form;


    function RegisteredUsersDeletePage() {
        parent::PageController();


        $this->form = new FormController();

        $this->form->addChild('delete', new ButtonController(new Delegate($this, 'delete')));
        $this->form->addChild('cancel', new ButtonController(new Delegate($this, 'cancel')));
        $this->form->setDefaultChild('cancel');

        $this->form->registerOnLoadListener(new Delegate($this, 'load'));

        $this->addChild('DeleteForm', $this->form);
        $this->setDefaultChild('DeleteForm');

        $this->registerOnActivateListener(new Delegate($this, 'guard'));

        $this->setDefaultView(new Handle('LayoutFormView', array('/adm/users/registered.delete.html')));
        $this->addView('success', new Handle('RedirectView', array('/')));
        $this->addView('cancel', new Handle('RedirectView', array('/')));
        $this->addView('notfound', new Handle('RedirectView', array('/')));
    }

    function guard(&$source, &$request, &$responseModel) {
        if (!$request->hasParameter('user_id')) {
            return 'notfound';
        }
    }

    function load(&$source, &$request, &$responseModel) {
    	$user_id = $request->getParameter('user_id');
        $record =& User::getRecord($user_id);
        if (is_null($record)) {
            return 'notfound';
        }

        $responseModel->import($record->export());
    }

    function cancel(&$source, &$request, &$responseModel) {
        return 'cancel';
    }

    function delete(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$login = $responseModel->get('login');
        	$user_id = $request->getParameter('user_id');
        	if ($user_id) {
            	User::delete($user_id);
            	$responseModel->set('msg','The user '.$login.' has been deleted!');
            	return 'success';
            } else return 'notfound';
        }
    }

}


?>